package com.examp.dy.dy.controller;



import com.examp.dy.dy.dao.BookRepository;
import com.examp.dy.dy.entities.Accounts;
import com.examp.dy.dy.entities.Book;
import com.examp.dy.dy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Controller


public class
Bookcontroller {
    @Autowired
    BookService bookService;
@Autowired
BookRepository bookRepository;
    @GetMapping("books")
    public String displayBook(Model model){
        List<Book> books = bookService.getAll();
        model.addAttribute("books",books);
        return "/main/book information";
    }
    @GetMapping("/entry")
    public  String displayBookForm  (Model model)
    {
       Book aBook=new Book();
        model.addAttribute("book",aBook);
        return  "/main/book entry";
    }

    @PostMapping("/save")
    public String creatBook(Model model, Book book){
    bookService.save(book);
    return "redirect:/books";
    }
    @GetMapping("/deleteBook")
    public String delete(@RequestParam("id")Long id){
        Long bookId = id;
        bookService.deleteById(bookId);
        return "redirect:/books";
    }
   @GetMapping("/search")
    public String search(Model model,@RequestParam("bookname")String name){
    String bookname = name;
    bookService.findBookByBookname(bookname);
    model.addAttribute("books",bookname);
    return "redirect:/books";
}
@GetMapping("/updateBook")
    public String update(@RequestParam("id") long bookId,Model model){
 Book theBook=bookService.findByBookId(bookId);
  model.addAttribute("book",theBook);
    return "/main/book entry";
}



}
